//后台任务的数组，记录任务名称
var appArray = new Array();
appArray[0] = '首页';
var preAppName = '';


/**
 * 打开应用
 * @param url:网址
 */
function openApp(url,name){

    preappName = name;
    var hadOpenMission = $('.app');
    var topbarMission = $('.topBarContent');
    var markOpen = $('.markOpen');
    $('.markOpen').removeClass('active');
    for (var i=0;i<hadOpenMission.length;i++){
        if (hadOpenMission[i].dataset.name == name){
            $('.app').removeClass('active');
            $('.topBarContent').removeClass('activityMission');
            hadOpenMission[i].className = 'app active';
            topbarMission[i].className = 'topBarContent activityMission';
            hadOpenMission[i].src = url;

            //设置小蓝点
            for (var j=0;j<markOpen.length;j++){
                if (markOpen[j].dataset.name == name){
                    markOpen[j].className = 'markOpen active';
                } else {
                    markOpen[j].className = 'markOpen';
                }
            }
            return;
        }
    }

    for (var j=0;j<markOpen.length;j++){
        if (markOpen[j].dataset.name == name){
            markOpen[j].className = 'markOpen active'
        } else {
            markOpen[j].className = 'markOpen';
        }
    }

    console.log("数组尝试前："+appArray.length);
    //添加任务进入数组
    appArray[appArray.length] = name;
    console.log("数组尝试："+appArray.length);

    //添加任务到显示区域
    $('.app').removeClass('active');
    var mission = "<iframe src='"+url+"' frameborder='no' scrolling='auto' id='futermarket' allowtransparency='true' class='app active' data-name='"+name+"'></iframe>";
    $('#showContent').append(mission);


    //设置小蓝点
    //$('.markOpen').removeClass('active');
    //(obj.getElementsByClassName('markOpen'))[0].className = 'markOpen active';

    //设置添加顶部任务
    $('.topBarContent').removeClass('activityMission');
    /*var add = "<div class=\"fu-mission-item d-none d-lg-block\" onclick=\"openMission('"+name+"',this)\" style=\"position: relative\" data-name='"+name+"'>\n" +
        "                    <label class=\"topBarContent activityMission\" data-name='"+name+"'>\n" +
        "                        <span>"+name+"</span>\n" +
        "                    </label>\n" +
        "                    <img src=\"/img/appLogo/back.png\" width=\"16\" onclick='removeMission(\""+name+"\")' class='fu-back-btn'/>\n" +
        "                </div>";*/
    var add = "<div class=\"fu-mission-item d-none d-lg-block\" data-name='"+name+"'>\n" +
        "                    <label class=\"topBarContent activityMission\"  onclick=\"openMission('"+name+"',this)\" data-name='"+name+"'>\n" +
        "                        <span>"+name+"</span>\n" +
        "                    </label>\n" +
        "                    <img src=\"/img/appLogo/back.png\" width=\"16\" class=\"fu-back-btn\" onclick=\"removeMission('"+name+"')\"/>\n" +
        "                </div>";

    $('#fu-app-item').append(add);
}


/**
 * 打开选中的后台任务
 * @param name：任务名称
 * @param obj：顶部任务的选择器
 */
function openMission(name,obj){
    //获取对应的后台应用,并进行显示
    var hadOpenMission = $('.app');
    var topbarMission = $('.topBarContent');
    var markOpen = $('.markOpen');
    for (var i=0;i<hadOpenMission.length;i++){
        if (hadOpenMission[i].dataset.name == name){
            hadOpenMission[i].className = 'app active';
            topbarMission[i].className = 'topBarContent activityMission'
        } else {
            hadOpenMission[i].className = 'app';
            topbarMission[i].className = 'topBarContent'
        }
    }

    //设置应用栏的小蓝点
    for (var i=0;i<markOpen.length;i++){
        if (markOpen[i].dataset.name == name){
            markOpen[i].className = 'markOpen active';
        } else {
            markOpen[i].className = 'markOpen';
        }
    }


}



function test() {
    $(".topBarContent").addClass("activityMission")
}

/**
 * 删除任务
 * @param name:应用名称
 */
function removeMission(name) {
    console.log(name);
    if (appArray.length<=1){
        return;
    }


    //$('.topBarContent').addClass('activityMission');
    //获取顶部应用栏的后台任务
    var hadOpenMission = $('.app');
    var topbarMission = $('.fu-mission-item');
    for (var i=0;i<topbarMission.length;i++){
        if (topbarMission[i].dataset.name == name){
            topbarMission[i].remove();

            hadOpenMission[i].remove();
        }
    }


    //获取上一个任务
    var index = 0;
    for (var i=0;i<appArray.length;i++){
        if (appArray[i] == name){
            if (i==0){
                preAppName = appArray[1];
            }else {
                preAppName = appArray[i-1];
            }
            index = i;
        }
    }
    appArray.splice(index,1);


    //设置上一个任务为前台任务
    $('.app').removeClass('active');
    $('.topBarContent').removeClass('activityMission');


    $(".app").each(function () {
        if (this.dataset.name == preAppName){
            $(this).addClass('active');
        }
    });

    $(".topBarContent").each(function () {
        if (this.dataset.name == preAppName){
            $(this).addClass('activityMission');
        }
    });

    $('.markOpen').removeClass('active');
    $(".markOpen").each(function () {
        if (this.dataset.name == preAppName){
            $(this).addClass('active');
        }
    });

}


/*
* 跳转到登录界面
*
* */
function sengToLogin(){
    window.location.href="/login"
}

function loadCss(){

    $.ajax({
        type: "post",
        url: "/futer/reLoadCss",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response, ifo) {

            if (response=='请求被拒绝，无权限访问'){
                parent.sengToLogin();
                return;
            }

            (document.getElementById("body")).style.backgroundColor="${config.bgforframe}";
            (document.getElementById("bottomBar")).style.backgroundColor="${config.bgforappbar}";
            (document.getElementById("topBar")).style.backgroundColor="${config.bgformissionbar}";
            var span=document.getElementsByTagName("span");
            for (var i=0;i<span.length;i++){
                span[i].style.color="${config.fontcolor}";
            }
        }, error: function (err) {
            alert("修改失败！");
        }
    })

}

function sendTo(){

}


/**
 * 全屏显示中间的内容
 */
function fullscreen(){
    var left = document.getElementById("fu-applist");
    var topBar = document.getElementById("topBar");
    if (left.style.display == 'block' || topBar.style.display == 'block'){
        left.style.display = 'none';
        topBar.style.display = 'none';
    } else {
        left.style.display = 'block';
        topBar.style.display = 'block';
    }
}

/*
*获取菜单栏软件
*/
/*
function reFreshApp(){
    $.ajax({
        type: "post",
        url: "/futer/getApp",
        dataType: "json",
        success: function (response) {
            console.log("成功"+JSON.stringify(response));
            /!*$(".app").append("<li class=\"appMission\" onclick=\"openApp('"+response.appUrl+"','"+response.appname+"')\">\n" +
                "                                        <div class=\"fu-appimgparent\">\n" +
                "                                            <img src='"+response.appImg+"'>\n" +
                "                                        </div>\n" +
                "                                        <span style=\"font-size: 12px\">"+response.appname+"</span>\n" +
                "                                        <div class=\"markOpen active\" data-name='"+response.appname+"'></div>\n" +
                "                                    </li>");*!/

        }, error: function (err) {
            console.log("错误日志："+err);
        }
    })
}*/
